package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.BorrowDao;
import com.example.librarysystem.Dao.BorrowerDao;
import com.example.librarysystem.Dao.CommentDao;
import com.example.librarysystem.Dao.OrderDao;
import com.example.librarysystem.entity.Borrow;
import com.example.librarysystem.entity.Borrower;
import com.example.librarysystem.entity.Comment;
import com.example.librarysystem.entity.Order;
import net.bytebuddy.agent.builder.AgentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service("borrowerService")
public class BorrowerServiceImpl implements BorrowerService{
    @Autowired
    private BorrowerDao borrowerDao;
    @Autowired
    private BorrowDao borrowDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CommentDao commentDao;

    @Override
    public Integer selectCount(){
        return borrowerDao.selectCount();
    }

    @Override
    public Borrower selectByName(String name){
        return borrowerDao.selectByName(name);
    }

    @Override
    public Borrower selectById(int id){
        return borrowerDao.selectById(id);
    }

    @Override
    @Transactional(timeout = 30)
    public int updateBorrowerAddress(Borrower borrower){
        borrowerDao.updateBorrowerAddress(borrower);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateBorrowerPhone(Borrower borrower){
        borrowerDao.updateBorrowerPhone(borrower);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateBorrowerEmail(Borrower borrower){
        borrowerDao.updateBorrowerEmail(borrower);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateBorrowerPassword(Borrower borrower){
        borrowerDao.updateBorrowerPassword(borrower);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateBorrowerAddressAndPhone(Borrower borrower){
        borrowerDao.updateBorrowerAddressAndPhone(borrower);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public List<Borrower> selectAll(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return borrowerDao.selectAll(startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Borrower> selectByNameAndPhone(String name, String phoneNum, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return borrowerDao.selectByNameAndPhone(name, phoneNum,  startCurrentPage,pageSize);
        }
    }
    @Override
    public int selectByNameAndPhoneCount(String name, String phoneNum){
        return borrowerDao.selectByNameAndPhoneCount(name,phoneNum);
    }
    @Override
    @Transactional(timeout = 60)
    public int deleteById(int id){
        Date now = new Date(new java.util.Date().getTime());
        List<Borrow> borrows = borrowDao.selectByBorrowerId(id);
        List<Order> orders = orderDao.selectByBorrowerId(id, now);
        for (Borrow borrow : borrows) {
            borrowDao.updateEndDate(now, borrow.getId());
            borrowDao.updateIsReturn(true,borrow.getId());
        }
        for (Order order : orders){
            orderDao.deleteOrderById(order.getId());
        }
        borrowerDao.deleteById(id);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int insertBorrower(Borrower borrower){
        borrowerDao.insertBorrower(borrower);
        return 0;
    }
}
